import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path:"/",
        name:"Main",
        component:() => import("../views/Main.vue"),
        children:[
          {
            path:"/",
            name:"MainHome",
            component:() => import("../views/MainHome.vue")
          },
          {
            path:"/photo",
            name:"Photo",
            component:() => import("../views/Photo.vue")
          },
          {
            path:"/list",
            name:"List",
            component:() => import("../views/StoreList.vue")
          }
        ]
      },
      {
        path:"/store",
        name:"Store",
        component:() => import("../views/Store.vue")
      },
      {
        path:"/order",
        name:"Order",
        component:() => import("../views/OrderForm.vue")
      },
      {
        path:"/member",
        name:"Member",
        component:() => import("../views/Member.vue")
      },
      {
        path:"/setting",
        name:"Setting",
        component:() => import("../views/Setting.vue")
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path:"/login",
    name:"Login",
    component:() => import("../views/Login.vue")
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  var userLoginStatus = localStorage.getItem("token");
  if(to.name === "Login"){
    next();
  }else{
    if(userLoginStatus){
      next();
    }else{
      next("/login")
    }
  }
})

export default router
